import {createRouter, createWebHistory} from 'vue-router'
import {Message} from "@arco-design/web-vue";
import {useStore} from "@/stores/counter";


const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        // 直接访问
        {
            path: '/',
            redirect: "/admin"
        },
        // admin登录页面
        {
            path: '/admin/login',
            name: 'admin_login',
            component: () => import('@/views/admin/login/index.vue'),
        },
        // 这里后台admin
        {
            path: '/admin',
            name: 'admin',
            meta: {
                title: "index",
                isAdmin: true,
            },
            component: () => import('../views/admin/index.vue'),
            children: [
                {
                    path: "",
                    name: "admin_home",
                    meta: {
                        title: "首页"
                    },
                    component: () => import('../views/admin/home/index.vue'),
                },
                {
                    path: "my_center", // 这里不能用/因为是相对路劲
                    name: "admin_my_center",
                    meta: {
                        title: "个人中心"
                    },
                    children: [
                        {
                            path: "info",
                            name: "admin_my_center_info",
                            meta: {
                                title: "我的信息"
                            },
                            component: () => import('../views/admin/my_center/my_info.vue'),
                        },
                    ]
                },
                {
                    path: "article",
                    name: "admin_article",
                    meta: {
                        title: "文章管理"
                    },
                    children: [
                        {
                            path: "list",
                            name: "admin_article_list",
                            meta: {
                                title: "文章列表"
                            },
                            component: () => import('../views/admin/article/article_list.vue'),
                        },
                    ]
                },
                {
                    path: "user",
                    name: "admin_user",
                    meta: {
                        title: "用户管理"
                    },
                    children: [
                        {
                            path: "list",
                            name: "admin_user_list",
                            meta: {
                                title: "用户列表"
                            },
                            component: () => import('../views/admin/user/user_list.vue'),
                        },
                    ]
                },
                {
                    path: "chat_group",
                    name: "admin_chat_group",
                    meta: {
                        title: "群聊管理"
                    },
                    children: [
                        {
                            path: "list",
                            name: "admin_chat_group_list",
                            meta: {
                                title: "聊天记录"
                            },
                            component: () => import('../views/admin/chat_group/chat_list.vue'),
                        },
                    ]
                },
                {
                    path: "system",
                    name: "admin_system",
                    meta: {
                        title: "系统管理"
                    },
                    children: [
                        {
                            path: "menu_list",
                            name: "admin_system_menu_list",
                            meta: {
                                title: "菜单管理"
                            },
                            component: () => import('../views/admin/system/menu_list.vue'),
                        },
                        {
                            path: "log_list",
                            name: "admin_system_log_list",
                            meta: {
                                title: "日志管理"
                            },
                            component: () => import('../views/admin/system/log_list.vue'),
                        },
                        {
                            path: "promotion_list",
                            name: "admin_system_advert_list",
                            meta: {
                                title: "广告管理"
                            },
                            component: () => import('../views/admin/system/promotion_list.vue'),
                        },
                    ]
                },

            ]
        },
    ]
})

export default router

// 跳转之前
router.beforeEach((to, from, next) => {
    const store = useStore()
    if (to.meta.isAdmin && !store.isAdmin) {
        // 需要登录
        Message.warning("请您登录")
        router.push({name: 'admin_login'})
        return
    }
    next()
})

// 跳转之后
// router.afterEach((to, from, next) => {
//     const store = useStore()
//     if (to.name === "admin_login" && store.isAdmin) {
//         router.push({name: from.name as string})
//         return
//     }
// })
